package utils.database;

import java.sql.Connection;
import java.sql.SQLException;

import org.postgresql.ds.PGConnectionPoolDataSource;

public class DB {
	

	private static Connection connection;
	
	/*
	 * Connection Info
	 */
	private static String username = "postgres";
	private static String password = "lahti";
	private static String serverName = "127.0.0.1";
	
	private static int port = 1521;

	public static String getUsername() {
		return username;
	}

	public static void setUsername(String username) {
		DB.username = username;
	}

	public static String getPassword() {
		return password;
	}

	public static void setPassword(String password) {
		DB.password = password;
	}

	public static String getServerName() {
		return serverName;
	}

	public static void setServerName(String serverName) {
		DB.serverName = serverName;
	}
	
	public static int getPort() {
		return port;
	}

	public static void setPort(int port) {
		DB.port = port;
	}
	
	public static Connection getConnection() throws SQLException {
		
		if(connection == null || connection.isClosed())
		{			
			connection = createConnection();
		}
		
		return connection;
	}
	
	private static Connection createConnection() throws SQLException
	{
		PGConnectionPoolDataSource ds = new PGConnectionPoolDataSource();
		
		ds.setServerName("localhost");
		ds.setDatabaseName("dewey");
		ds.setUser(username);
		ds.setPassword(password);
		return ds.getConnection();		
	}
}
